<?php

/**
 * User: Allen
 * Date: 15-10-12
 * 登录控制器
 */
class Login extends CI_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->model('admin_model');
		$this->load->model('user_model');
		$this->load->library('visitor');
		$this->lang->load('login');
	}

	/**
	 * @brief 后台登录
	 * @param Null
	 * @par 2015/10/12 Ver 1.00 Created by Allen
	 */
	public function index()
	{

		if ($_POST) {
			$username = $this->input->post('username');
			$password = $this->input->post('password');
			$source = $this->input->post('source');
			if ($source == 2) {
				$admin = $this->admin_model->checkAdmin(array('username' => $username));
				if ($admin) {
					$admin['admin_type'] = 'admin';
					$admin['status'] = 1;
				}
			} else {
				$admin = $this->user_model->checkUser(array('username' => $username));
				if ($admin) {
					$admin['admin_type'] = 'user';
				}
			}

			if ($admin && md5($password) == $admin['password'] && $admin['status'] == 1) {
				$user_data['admin'] = $admin;
				$this->visitor->assign($user_data);
				header("Location:http://" . $_SERVER['HTTP_HOST'] . "/admin/welcome");
			} else {
				if (empty($username)) {
					$this->data['username'] = $this->input->post('username');
					$this->data['password'] = $this->input->post('password');
					$this->data['source'] = $this->input->post('source');
					$this->data['error_msg'] = $this->lang->line('username_blankText');
				} else if (empty($password)) {
					$this->data['username'] = $this->input->post('username');
					$this->data['password'] = $this->input->post('password');
					$this->data['source'] = $this->input->post('source');
					$this->data['error_msg'] = $this->lang->line('password_blankText');
				} else {
					$this->data['username'] = $this->input->post('username');
					$this->data['password'] = $this->input->post('password');
					$this->data['source'] = $this->input->post('source');
					$this->data['error_msg'] = $this->lang->line('wrong_info');
				}
				$this->load->view('/admin/login.php', $this->data);
			}
		} else {
			$this->data['username'] = '';
			$this->data['password'] = '';
			$this->data['source'] = '2';
			$this->data['error_msg'] = '输入用户名密码';
			$this->load->view('/admin/login.php', $this->data);
		}
	}

	/**
	 * @brief 后台退出
	 * @param Null
	 * @par 2015/10/12 Ver 1.00 Created by Allen
	 */
	public function logout()
	{
		$this->visitor->signout();
		$this->data['error_msg'] = '请输入帐号密码';
		$this->data['username'] = '';
		$this->data['password'] = '';
		$this->data['source'] = 2;
		$this->load->view('/admin/login.php', $this->data);
	}

	// 	//修改密码
	// 	public function change_pswd() {
	// 		$admin = $this->visitor->get_info('admin');
	// 		$where['username'] = $admin['username'];

	// 		$old_pswd = $this->input->post('old_pswd');
	// 		$new_pswd = $this->input->post('new_pswd');

	// 		$res = $this->admin_model->checkAdmin($where);
	// 		if($res !='' && $res['password'] == $old_pswd){
	// 			$this->admin_model->editMerchant($where, array('password'=>$new_pswd));
	// 			exit(getJsonOutput(array('status'=>true, 'data'=>'修改成功！')));
	// 		}
	// 		else{
	// 			exit(getJsonOutput(array('status'=>false, 'data'=>'原始密码不正确！')));
	// 		}
	// 	}

	//  	public function signin(){
	//  		$this->data['error_msg'] = '请输入帐号密码';
	//  		$this->data['username'] = '';
	//  		$this->data['password'] = '';
	//  		$this->load->view('/www/login.php',$this->data);
	//  	}
}
